/**
 * Created by isoft-work 5 on 11/17/14.
 */

$(document).ready(function(){
//   event
   bindEventForgetPass();
});

function bindEventForgetPass() {
   ISS.EVENT.bindEvent('#forget_pass_send', 'click', function () {
      clearValidate();
      if(validate()){
         sendInfo();
      }
   });
}

function clearValidate() {
   ISS.DOM.remove('#message-error');
   ISS.DOM.html('#check_error_email', '');

   ISS.DOM.removeClass('#ForgotPasswordForm_email', 'error');
}

function validate() {
   var email, result;
   email = ISS.DOM.getVal('#ForgotPasswordForm_email');
   if(!email) {
      ISS.DOM.html('#check_error_email', 'Mục yêu cầu');
      ISS.DOM.addClass('#ForgotPasswordForm_email', 'error');
      result = false;
   }
   else {
      if(!ISS.VALIDATION.validateEmail(email, true)) {
         ISS.DOM.html('#check_error_email', 'Địa chỉ email không hợp lệ');
         ISS.DOM.addClass('#ForgotPasswordForm_email', 'error');
         result = false;
      }
      else {
         result = true;
      }
   }
   return result;
}

function sendInfo() {
   var user = {}, phanQuyen = {};
//   get info
   user.maTV = ISS.DOM.getVal('#ForgotPasswordForm_email');
   phanQuyen.thongTinThanhVien = user;
//   request
   ISS.BACKEND.request({
      url: '../../service/EditAccount/forgetPass/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(phanQuyen),
      success: function (data) {
         var responseContent = data.responseObject;
         sendInfoComplete(responseContent);
      }
   });
}

function sendInfoComplete(user) {
   if(user) {
      var htmlText = '<div id="message-error" class="box s-success mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Mật khẩu đã được gửi tới mail bạn</div>\
                            </div>';
      ISS.DOM.before('#form-forgot-password', htmlText);
   }
}